﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Samples.CustomControls;

namespace InkCanvasControls
{
    /// <summary>
    /// Interaction logic for CustomColour.xaml
    /// </summary>
    public partial class CustomColour : UserControl
    {
        #region Members
        private bool firstTime = true;
        #endregion

        #region Constructor
        public CustomColour()
        {
            InitializeComponent();
        }
        #endregion

        #region Dependency Properties
        public Color ChosenColour
        {
            get { return (Color)GetValue(ChosenColourProperty); }
            set { SetValue(ChosenColourProperty, value); }
        }

        public static readonly DependencyProperty ChosenColourProperty =
            DependencyProperty.Register("ChosenColour", typeof(Color), typeof(CustomColour), new PropertyMetadata(Colors.Green));
        #endregion

        #region RoutedEvents
        public static readonly RoutedEvent ColourSelectedEvent = EventManager.RegisterRoutedEvent(
            "ColourSelected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomColour));

        // Provide CLR accessors for the event
        public event RoutedEventHandler ColourSelected
        {
            add { AddHandler(ColourSelectedEvent, value); }
            remove { RemoveHandler(ColourSelectedEvent, value); }
        }

        // This method raises the ColourSelected event
        void RaiseColourSelectedEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomColour.ColourSelectedEvent);
            RaiseEvent(newEventArgs);
        }
        #endregion

        #region Custom Colours support
        private void SelectColour_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (SelectColour.Items.Count == 1)
            {
                if (SelectColour.SelectedIndex == 0)
                {
                    //When "Add new..." is the only item showing it won't trigger a SelectionChanged event, so...
                    AddNewColourUsingColorPicker();
                }
            }
        }

        private void SelectColour_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!firstTime)
            {
                if (SelectColour.SelectedIndex == 0)
                {
                    AddNewColourUsingColorPicker();
                }
                else
                {
                    ChosenColour = SelectedColour();
                }
                RaiseColourSelectedEvent();
            }
            firstTime = false;
        }

        private Color SelectedColour()
        {
            Border textBorder = SelectColour.SelectedItem as Border;
            if (textBorder != null)
            {
                TextBlock textItem = textBorder.Child as TextBlock;
                if (textItem != null)
                {
                    SolidColorBrush brush = textItem.Background as SolidColorBrush;
                    if (brush != null)
                    {
                        return brush.Color;
                    }
                }
            }
            return Colors.Black;
        }

        private void AddNewColourUsingColorPicker()
        {
            ColorPickerDialog colourPicker = new ColorPickerDialog();
            colourPicker.Owner = Window.GetWindow(this);
            bool? dialogResult = colourPicker.ShowDialog();
            if (dialogResult != null)
            {
                if (dialogResult == true)
                {
                    AddNewColour("                 ", colourPicker.SelectedColor);  //Insert colour with no name, just background swatch
                }
            }
        }

        private void NewColour_Selected(object sender, RoutedEventArgs e)
        {
            ChosenColour = SelectedColour();
        }


        private void AddNewColour(string name, Color colour)
        {
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.Transparent);
            border.BorderThickness = new Thickness(0, 2, 0, 2);
            TextBlock text = new TextBlock();
            text.Text = name;
            text.Background = new SolidColorBrush(colour);
            border.Child = text;

            SelectColour.SelectedIndex = SelectColour.Items.Add(border);
            ChosenColour = colour;
        }

        #endregion
    }
}
